home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / i2scan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  7.4 KB  |  256 lines

  1. /* Copyright (C) 1989, 1990 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* i2scan.c */
  21. /* Level 2 token scanner (binary tokens) */
  22. #include "ghost.h"
  23. #include "arch.h"
  24. #include "errors.h"
  25. #include "alloc.h"
  26. #include "store.h"
  27. #include "stream.h"
  28. #include "i2btoken.h"
  29. #include "i2bseq.h"
  30. #include "i2num.h"
  31.  
  32. /* Import the system and user name tables */
  33. extern ref system_names, user_names;
  34.  
  35. /* Forward references */
  36. private    int    scan_binary_sequence(P2(stream *, ref *));
  37.  
  38. /* Scan a binary token.  Called from the main scanner */
  39. /* when it encounters an ASCII code 128 or above. */
  40. /****** THIS ROUTINE STILL NEEDS MANY EOF CHECKS ******/
  41. int
  42. scan_binary_token(register stream *s, ref *pref, char tcode)
  43. {    int num_format, code;
  44.     uint arg;
  45.     switch ( tcode )
  46.        {
  47.     case bt_seq_IEEE_msb:
  48.         s->num_format = num_msb + num_float_IEEE; goto bseq;
  49.     case bt_seq_IEEE_lsb:
  50.         s->num_format = num_lsb + num_float_IEEE; goto bseq;
  51.     case bt_seq_native_msb:
  52.         s->num_format = num_msb + num_float_native; goto bseq;
  53.     case bt_seq_native_lsb:
  54.         s->num_format = num_lsb + num_float_native;
  55. bseq:        return scan_binary_sequence(s, pref);
  56.     case bt_int32_msb:
  57.         num_format = num_msb + num_int32; goto num;
  58.     case bt_int32_lsb:
  59.         num_format = num_lsb + num_int32; goto num;
  60.     case bt_int16_msb:
  61.         num_format = num_msb + num_int16; goto num;
  62.     case bt_int16_lsb:
  63.         num_format = num_lsb + num_int16; goto num;
  64.     case bt_int8:
  65.         make_int(pref, (sgetc(s) ^ 128) - 128);
  66.         return 0;
  67.     case bt_fixed:
  68.         num_format = sgetc(s);
  69.         if ( !num_is_valid(num_format) )
  70.             return e_syntaxerror;
  71.         goto num;
  72.     case bt_float_IEEE_msb:
  73.         num_format = num_msb + num_float_IEEE; goto num;
  74.     case bt_float_IEEE_lsb:
  75.         num_format = num_lsb + num_float_IEEE; goto num;
  76.     case bt_float_native:
  77.         num_format = num_float_native;
  78. num:        s->num_format = num_format;
  79.         code = sget_encoded_number(s, pref);
  80.         switch ( code )
  81.            {
  82.         case t_integer: case t_real:
  83.             r_set_type(pref, code);
  84.             break;
  85.         case t_null: return e_syntaxerror;
  86.         default: return code;
  87.            }
  88.         return 0;
  89.     case bt_boolean:
  90.         arg = sgetc(s);
  91.         if ( arg & ~1 ) return e_syntaxerror;
  92.         make_bool(pref, arg);
  93.         return 0;
  94.     case bt_string_256:
  95.         arg = sgetc(s); goto str;
  96.     case bt_string_64k_msb:
  97.         arg = sgetc(s); arg = (arg << 8) + sgetc(s); goto str;
  98.     case bt_string_64k_lsb:
  99.         arg = sgetc(s) << 8; arg += sgetc(s);
  100. str:       {    byte *str = alloc(arg, 1, "string token");
  101.         uint rcnt;
  102.         if ( str == 0 ) return e_VMerror;
  103.         rcnt = sread(s, str, arg);
  104.         if ( rcnt != arg) return e_syntaxerror;
  105.         make_tasv(pref, t_string, a_all, arg, bytes, str);
  106.        }    return 0;
  107.     case bt_litname_system:
  108.         *pref = system_names.value.refs[sgetc(s)]; return 0;
  109.     case bt_execname_system:
  110.         *pref = system_names.value.refs[sgetc(s)];
  111. xname:        r_set_attrs(pref, a_executable);
  112.         return 0;
  113.     case bt_litname_user:
  114.         *pref = user_names.value.refs[sgetc(s)]; return 0;
  115.     case bt_execname_user:
  116.         *pref = user_names.value.refs[sgetc(s)]; goto xname;
  117.     case bt_num_array:
  118.        {    ref *nap;
  119.         uint i;
  120.         num_format = sgetc(s);
  121.         if ( !num_is_valid(num_format) ) return e_syntaxerror;
  122.         s->num_format = num_format;
  123.         sgetshort(s, (short *)&arg);
  124.         nap = (ref *)alloc(arg, sizeof(ref), "number array token");
  125.         if ( nap == 0 ) return e_VMerror;
  126.         for ( i = 0; i < arg; i++ )
  127.            {    ref *np = nap + i;
  128.             int code = sget_encoded_number(s, np);
  129.             switch ( code )
  130.                {
  131.             case t_integer: case t_real:
  132.                 r_set_type(np, code);
  133.                 break;
  134.             case t_null: return e_syntaxerror;
  135.             default: return code;
  136.                }
  137.            }
  138.         make_tasv(pref, t_array, a_all, arg, refs, nap);
  139.        }    return 0;
  140.        }
  141.     return e_syntaxerror;
  142. }
  143.  
  144. /* Scan a binary object sequence. */
  145. private int
  146. scan_binary_sequence(register stream *s, ref *pref)
  147. {    uint top_size = sgetc(s);
  148.     ushort size;
  149.     uint max_array_index = top_size * sizeof(ref);
  150.     uint min_string_index = 0xffff;
  151.     byte *obj;
  152.     uint index;
  153. #if big_endian
  154. #  define must_swap_bytes num_is_lsb(s->num_format)
  155. #else
  156. #  define must_swap_bytes !num_is_lsb(s->num_format)
  157. #endif
  158.     sgetshort(s, (short *)&size);
  159.     obj = alloc(1, size, "binary sequence token");
  160.     if ( obj == 0 ) return e_VMerror;
  161.     if ( sread(s, obj, size) != size ) return e_syntaxerror;
  162.     /* Convert embedded refs */
  163.     for ( index = 0; index < max_array_index;
  164.          index += sizeof(bin_seq_obj)
  165.         )
  166.        {    ref *ap = (ref *)obj + index;
  167.         bin_seq_obj ob;
  168.         byte bt;
  169.         ob = *(bin_seq_obj *)ap;
  170. #define swap_size()\
  171.   if ( must_swap_bytes )\
  172.     bt = ob.size.b[0], ob.size.b[0] = ob.size.b[1], ob.size.b[1] = bt
  173. #define swap_value()\
  174.   if ( must_swap_bytes )\
  175.     bt = ob.value.b[0], ob.value.b[0] = ob.value.b[3], ob.value.b[3] = bt,\
  176.     bt = ob.value.b[1], ob.value.b[1] = ob.value.b[2], ob.value.b[2] = bt
  177.         switch ( ob.tx & 0x7f )
  178.            {
  179.         case bs_null:
  180.             make_null(ap); break;
  181.         case bs_integer:
  182.             swap_value();
  183.             make_int(ap, ob.value.w);
  184.             break;
  185.         case bs_real:
  186.             swap_value();
  187.             /****** DON'T HANDLE NON-NATIVE REAL YET ******/
  188.             make_real(ap, ob.value.f);
  189.             break;
  190.         case bs_boolean:
  191.             swap_value();
  192.             make_bool(ap, (ob.value.w == 0 ? 0 : 1));
  193.             break;
  194.         case bs_string:
  195.             swap_size(); swap_value();
  196.             if ( ob.value.w < max_array_index ||
  197.                 ob.value.w + ob.size.w > size
  198.                )
  199.                 return e_syntaxerror;
  200.             if ( ob.value.w < min_string_index )
  201.                 min_string_index = (uint)ob.value.w;
  202.             make_tasv(pref, t_string, (ob.tx < 128 ? a_all :
  203.                   a_all + a_executable), ob.size.w,
  204.                   bytes, obj + ob.value.w);
  205.             break;
  206.         case bs_name:
  207.         case bs_eval_name:
  208.             swap_size(); swap_value();
  209.             switch ( ob.size.w )
  210.                {
  211.             case 0:
  212.                 if ( (ulong)ob.value.w >= user_names.size )
  213.                     return e_syntaxerror;
  214.                 *ap = user_names.value.refs[ob.value.w];
  215.                 break;
  216.             case 0xffff:
  217.                 if ( (ulong)ob.value.w >= system_names.size )
  218.                     return e_syntaxerror;
  219.                 *ap = system_names.value.refs[ob.value.w];
  220.                 break;
  221.             default:
  222.                 if ( ob.value.w < max_array_index ||
  223.                     ob.value.w + ob.size.w > size
  224.                    )
  225.                     return e_syntaxerror;
  226.                 if ( ob.value.w < min_string_index )
  227.                     min_string_index = (uint)ob.value.w;
  228.                 name_ref(obj + ob.value.w, ob.size.w, ap, 0);
  229.                }
  230.             if ( (ob.tx & 0x7f) == bs_eval_name )
  231.                 /****** LOOK UP THE NAME ******/
  232.                     ;
  233.             if ( ob.tx >= 128 ) r_set_attrs(ap, a_executable);
  234.             break;
  235.         case bs_array:
  236.             swap_size(); swap_value();
  237.             if ( ob.value.w + ob.size.w > min_string_index ||
  238.                 ob.value.w & (sizeof(bin_seq_obj) - 1)
  239.                )
  240.                 return e_syntaxerror;
  241.             max_array_index = max(max_array_index,
  242.                           ob.value.w + ob.size.w);
  243.             make_tasv(ap, t_array, (ob.tx < 128 ? a_all :
  244.                   a_all + a_executable), ob.size.w,
  245.                   refs, (ref *)(obj + ob.value.w));
  246.             break;
  247.         case bs_mark:
  248.             make_mark(ap); break;
  249.         default:
  250.             return e_syntaxerror;
  251.            }
  252.        }
  253.     make_tasv(pref, t_array, a_all, top_size, refs, (ref *)obj);
  254.     return 0;
  255. }
  256.